package com.cc.mall.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.common.utils.ResponseData;
import com.cc.mall.dao.ProductDao;
import com.cc.mall.entity.OnlineOrderEntity;
import com.cc.mall.feign.UserFeign;
import com.cc.mall.vo.OrderDetail;
import com.cc.user.entity.ScheduleEntity;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.mall.entity.OfflineOrderEntity;
import com.cc.mall.service.OfflineOrderService;
import com.cc.common.utils.R;



/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-09 20:57:52
 */
@RestController
@RequestMapping("mall/offlineorder")
@Api(tags="线下订单")
public class OfflineOrderController {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private OfflineOrderService offlineOrderService;

    /**
     * 列表
     */


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OfflineOrderEntity offlineOrder = offlineOrderService.getById(id);
        return R.ok().put("offlineOrder",offlineOrder);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody OfflineOrderEntity offlineOrder){
		offlineOrderService.save(offlineOrder);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody OfflineOrderEntity offlineOrder){
		offlineOrderService.updateById(offlineOrder);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		offlineOrderService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/updateOfflineOrderStatus")
    public R updateOfflineOrderStatus(@RequestParam("id") Long id,
                                      @RequestParam("status") Integer status){
        OfflineOrderEntity offlineOrderEntity = offlineOrderService.getById(id);
        offlineOrderEntity.setOfflineOrderStatus(status);
        offlineOrderService.updateById(offlineOrderEntity);
        if(status == 2){
            ScheduleEntity scheduleEntity = new ScheduleEntity();
            scheduleEntity.setUid(offlineOrderEntity.getCid());
            scheduleEntity.setCreatetime(new Date());
            scheduleEntity.setScheduleStatus(0);
            scheduleEntity.setContent("您的订单【"+id+"】【已发货】");
            userFeign.save(scheduleEntity);
        }
        return R.ok();
    }

    @ApiOperation("改线下订单的价格")
    @GetMapping("/gaijia")
    public R gaijia(@ApiParam("线下订单id") @RequestParam("id") Long id,
                    @ApiParam("更改的价格") @RequestParam("price") String price){
        OfflineOrderEntity offlineOrderEntity = new OfflineOrderEntity();
        offlineOrderEntity.setId(id);
        offlineOrderEntity.setAmountsPayable(new BigDecimal(price));
        offlineOrderService.updateById(offlineOrderEntity);
        return R.ok();
    }

    @GetMapping("/getOfflineOrderByCid/{cid}")
    public R getOfflineOrderByCid(@PathVariable("cid") Long cid){
        return R.ok().put("list",offlineOrderService.list(new QueryWrapper<OfflineOrderEntity>().eq("cid",cid)));
    }

    @ApiOperation("获取线下订单详情")
    @GetMapping("/getofflineOrderDetailByOrderId")
    public ResponseData<List<OrderDetail>> getofflineOrderDetailByOrderId(@ApiParam("订单id") @RequestParam("orderId") Long orderId){
        List<OrderDetail> orderDetails = offlineOrderService.getOnlineOrderDetailByOrderId(orderId);
        return ResponseData.success(orderDetails);
    }

    // 后台线上订单接口
    @GetMapping("/getOfflineOrderList")
    public R getOfflineOrderList(@RequestParam Map<String,Object> params){
        Page<OfflineOrderEntity> page = offlineOrderService.getOfflineOrderList(params);
        return R.ok().put("page",page);
    }
}
